package com.easyfun.healthmagicbox.util;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

import com.easyfun.healthmagicbox.R;
//import com.google.ads.AdRequest;
//import com.google.ads.AdSize;
//import com.google.ads.AdView;

public class PhoneNetworkUtility {
	protected static final String TAG = PhoneNetworkUtility.class
			.getSimpleName();
	Context context;
	ConnectivityManager connectMgr;

	public PhoneNetworkUtility(Context context) {
		this.context = context;
		connectMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	}

	/**
	 * 检测网络是否连接
	 * 
	 * @return
	 */
	public boolean checkNetworkState() {
		boolean flag = false;
		// 得到网络连接信息
		// 去进行判断网络是否连接
		if (connectMgr.getActiveNetworkInfo() != null) {
			flag = connectMgr.getActiveNetworkInfo().isAvailable();
		}
		if (!flag) {
			setNetwork();
		} else {
			isGPRSNetworkAvailable();
			isWifiNetworkAvailable();
		}

		return flag;
	}

	/**
	 * 网络未连接时，调用设置方法
	 */
	private void setNetwork() {
		Toast.makeText(context, "wifi is closed!", Toast.LENGTH_SHORT).show();

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("网络提示信息");
		builder.setMessage("网络不可用，如果继续，请先设置网络！");
		builder.setPositiveButton("设置", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = null;
				/**
				 * 判断手机系统的版本！如果API大于10 就是3.0+ 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
				 */
				if (android.os.Build.VERSION.SDK_INT > 10) {
					intent = new Intent(
							android.provider.Settings.ACTION_WIFI_SETTINGS);
				} else {
					intent = new Intent();
					ComponentName component = new ComponentName(
							"com.android.settings",
							"com.android.settings.WirelessSettings");
					intent.setComponent(component);
					intent.setAction("android.intent.action.VIEW");
				}
				context.startActivity(intent);
			}
		});

		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.create();
		builder.show();
	}

	/**
	 * 网络已经连接，然后去判断是wifi连接还是GPRS连接 设置一些自己的逻辑调用
	 */
	public boolean isGPRSNetworkAvailable() {

		boolean status = false;
		State gprs = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
			Toast.makeText(context, "network is open! gprs", Toast.LENGTH_SHORT)
					.show();
			return status;
		}
		return status;
	}

	/**
	 * 网络已经连接，然后去判断是wifi连接还是GPRS连接 设置一些自己的逻辑调用
	 */
	public boolean isWifiNetworkAvailable() {
		boolean status = false;
		State wifi = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		// 判断为wifi状态下才加载广告，如果是GPRS手机网络则不加载！
		if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
			Toast.makeText(context, "network is open! wifi", Toast.LENGTH_SHORT)
					.show();
			status = true;
			// loadAdmob();
		}
		return status;
	}

	/**
	 * 在wifi状态下 加载admob广告
	 */
	// private void loadAdmob(){
	// ll = (LinearLayout) findViewById(R.id.load_ads);
	// ll.removeAllViews();
	// adsView = new AdView(this, AdSize.BANNER, "a15194a1ac9505d");
	// ll.addView(adsView);
	//
	// adsView.loadAd(new AdRequest());
	// }

	BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			NetworkInfo mobNetInfo = connectMgr
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiNetInfo = connectMgr
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
				Log.i(TAG, "unconnect");
				// unconnect network
			} else {
				// connect network
			}
		}
	};

	void registerReceiver() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		context.registerReceiver(connectionReceiver, intentFilter);
	}

	void unRegisterReceiver() {
		if (connectionReceiver != null) {
			context.unregisterReceiver(connectionReceiver);
		}
	}

	public static boolean isWiFiActive(Context inContext) {
		WifiManager mWifiManager = (WifiManager) inContext
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
		int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
		if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
			System.out.println("**** WIFI is on");
			return true;
		} else {
			System.out.println("**** WIFI is off");
			return false;
		}
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			System.out.println("**** newwork is off");
			return false;
		} else {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info == null) {
				System.out.println("**** newwork is off");
				return false;
			} else {
				if (info.isAvailable()) {
					System.out.println("**** newwork is on");
					return true;
				}

			}
		}
		System.out.println("**** newwork is off");
		return false;
	}
}
